package club.caihappy.springsecurity.express;

import club.caihappy.springsecurity.pojo.LoginUser;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义权限校验方法
 * Created by DELL on 2022/9/20
 **/
@Component("ex")
public class PatrickExpressionRoot {

    public boolean hasAuthority(String authority) {
        //获取用户权限列表
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<String> permissions = loginUser.getPermissions();
        //判断用户权限中是否存在authority
        return permissions.contains(authority);
    }

}
